<template>
  <div>
    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="$parent.open" width="780px" append-to-body>
      <div class="center_dialog">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="收信人" prop="userIds">
            <el-select v-model="form.userIds" multiple @change="SelectChange()" placeholder="请选择收信人" style="width:100%">
              <el-option v-for="(dict, index) in UserList" :key="index" :label="dict.userName" :value="dict.userId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="发送内容" prop="content">
            <el-input type="textarea" v-model="form.content" :rows="4" placeholder="请输入发送内容" />
          </el-form-item>
        </el-form>
      </div>
      <div align="center" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="() => $parent.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getuserlist } from '@/api/equipment/equipment.js'
import { PostCreatedmessage } from '@/api/system/message.js'
export default {
  props: ['title', 'rowObj'],
  components: {
  },
  data () {
    return {
      form: {
        "userIds": [], //接收者ID集合(null为全部用户)
        "content": undefined, //发送内容
      },
      UserList: [],
      rules: {
        userIds: { required: true, message: '必填', trigger: 'blur' },
        content: { required: true, message: '必填', trigger: 'blur' },
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      let data = await getuserlist()
      this.UserList = data.data
      this.UserList.unshift({ userName: '全部用户', userId: -1 })
    },
    SelectChange () {
      for (let index = 0; index < this.form.userIds.length; index++) {
        const element = this.form.userIds[index];
        if (element === -1) {//包含全部用户
          this.form.userIds = [-1]
          return
        }
      }

    },
    //提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.title === '修改广告') {//修改

          } else {//新增
            let params = {
              ...this.form,
              userIds: this.form.userIds[0] === -1 ? null : this.form.userIds
            }
            PostCreatedmessage(params).then(res => {
              if (res.code === 200) {
                this.$message.success(res.msg)
                this.$parent.open = false
                this.$parent.handleUpdate()
              }
            })
          }
        }
      });
    },
  }
}
</script>

<style lang="scss" scoped>
.center_dialog {
  // text-align: center;
  width: 50%;
  margin: 0px auto;
}
</style>